<?php


namespace StarCloud\Library\Sdk;

use Framework\DiServices\HttpClient;
use Phalcon\Di;
use StarCloud\Library\ArraySort;
use StarCloud\Library\HttpCurl;

class QinronMedia
{
    use HttpCurl;

    /* @var $httpClient HttpClient */
    private $httpClient = null;

    private $token = "";
    private $appId = "qr_777d1f4eef554cc7380f544d7af08a35";
    private $app_secret = "43EF19A44A95E5595650BB487BA7D2C3";

    private $domain = "https://distributionapi.qinronmedia.com";

    public function __construct()
    {
        $this->httpClient = Di::getDefault()->get('httpClient');
        $this->cacheClass = Di::getDefault()->get('redis');
    }


    public function getHeader()
    {
        $token = $this->getCacheToken();
        if ($token) {
            $this->token = $token;
        } else {

            $body = [
                'app_id' => $this->appId,
                'app_secret' => $this->app_secret,
            ];

            $response = $this->httpClient->sendPostRequest($this->domain . '/api/v1/user/login/key', $body, [
                'Content-Type' => 'application/json',
            ]);

            $json = json_decode($response->getBody()->getContents(), true);
            if (isset($json['code']) && $json['code'] == 200) {
                $this->token = $json['data']['access_token'];
                $this->setCacheToken($json['data']['access_token'], $json['data']['expires_in'] - 1000);
            }
        }
        return [
            'Authorization' => 'Bearer ' . $this->token,
            'Content-Type' => 'application/json',
        ];
    }

    public function setCacheToken($token, $expire)
    {
        $keyName = __CLASS__ . ":token";
        $this->cacheClass->set($keyName, $token, $expire);
    }
    public function getCacheToken()
    {
        $keyName = __CLASS__ . ":token";
        return $this->cacheClass->get($keyName);
    }

    public function list($page, $size)
    {
        $body = [
            'page' => $page,
            'page_size' => $size,
        ];
        $response = $this->httpClient->sendPostRequest($this->domain . '/api/v1/open/video/list', $body, $this->getHeader());

        $json = json_decode($response->getBody()->getContents(), true);
        if (isset($json['code']) && $json['code'] == 200) {
            return $json['data']['list'] ?? [];
        }

        return [
            'list' => [],
        ];
    }

    public function videoDetail($videoId)
    {
        $video = $this->getCacheVideo($videoId);

        if (!empty($video)) {
            return  $video;
        }
        $body = [
            'video_id' => $videoId,
        ];
        $response = $this->httpClient->sendPostRequest($this->domain . '/api/v1/open/video/index', $body, $this->getHeader());

        $json = json_decode($response->getBody()->getContents(), true);
        if (isset($json['code']) && $json['code'] == 200) {
            $this->setCacheVideo($videoId, $json['data']);
            return $json['data'];
        }

        return false;
    }

    public function createLink($videoId, $video_index)
    {
        $body = [
            'video_id' => $videoId,
            'video_index' => $video_index,
        ];
        $response = $this->httpClient->sendPostRequest($this->domain . '/api/v1/open/create/link', $body, $this->getHeader());

        $json = json_decode($response->getBody()->getContents(), true);
        if (isset($json['code']) && $json['code'] == 200) {
            return $json['data'];
        }

        return false;
    }

    public function setCacheVideo($videoId, $info) {

        if (is_array($info)) {
            $info = json_encode($info);
        }

        $this->cacheClass->set($this->getVideoCacheKey($videoId), $info, 86400);

        return json_decode($info, true);
    }

    public function getCacheVideo($videoId)
    {
        $video = $this->cacheClass->get($this->getVideoCacheKey($videoId));
        $video = json_decode($video, true);

        return empty($video) ? false : $video;
    }

    public function getVideoCacheKey($videoId): string
    {
        return __CLASS__. '::' .__FUNCTION__. ":" . $videoId;
    }
}